package com.kwedding.service.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jws.soap.SOAPBinding.Style;

import com.kwedding.bean.TB_Style;
import com.kwedding.dao.TB_StyleMapper;
import com.kwedding.dto.web.RequestGetStyleList;
import com.kwedding.dto.web.RequestStyle;
import com.kwedding.dto.web.RequestWebAddStyle;
import com.kwedding.dto.web.RequestWebDelStyle;
import com.kwedding.dto.web.RequestWebGetStyleList;
import com.kwedding.dto.web.RequestWebUpdateStyle;
import com.kwedding.dto.web.ResponseStyle;
import com.kwedding.dto.web.ResponseStyleSimpleList;
import com.kwedding.dto.web.ResponseWebStyleList;
import com.kwedding.util.CheckedUtil;
import com.kwedding.util.Constants;
import com.kwedding.util.CopyPageInfo;
import com.kwedding.util.CopyerObject;
import com.kwedding.util.IDGenerate;
import com.kwedding.util.LoggerParent;
import com.kwedding.util.RequestErrorBackException;
import com.kwedding.util.Resp_PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.qiniu.http.Response;

@Service
public class WebStyleService extends LoggerParent {

	@Autowired
	private TB_StyleMapper tb_StyleMapper;

	/**
	 * 后台端获取品牌风格列表
	 * 
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<ResponseStyleSimpleList> getStyleSimple(RequestGetStyleList getStyleList) {
		List<TB_Style> tb_Styles = tb_StyleMapper.getStyleList(getStyleList.getBrandId(), false);
		List<ResponseStyleSimpleList> responseStyleSimpleLists = new ArrayList<>();
		if (tb_Styles != null && tb_Styles.size() > 0) {
			tb_Styles.stream().forEach(r -> {
				ResponseStyleSimpleList responseStyleSimpleList = CopyerObject.copyerObject(r,
						ResponseStyleSimpleList.class);
				responseStyleSimpleList.setIdStr(r.getStyleId().toString());
				responseStyleSimpleLists.add(responseStyleSimpleList);
			});
		} else {
			return new ArrayList<>();
		}
		return responseStyleSimpleLists;

	}

	/**
	 * 
	 * 添加风格
	 * 
	 * @param webAddStyle
	 */
	@Transactional
	public void insertStyle(RequestWebAddStyle webAddStyle) {
		CheckedUtil.isNull(webAddStyle.getBrandId(), Constants.REQUEST_ERROR_1, "品牌不能为空");
		CheckedUtil.isNull(webAddStyle.getChineseName(), Constants.REQUEST_ERROR_1, "中文名称不能为空");
		CheckedUtil.isNull(webAddStyle.getEnglishName(), Constants.REQUEST_ERROR_2, "英文名称不能为空");
		/*
		 * CheckedUtil.isNull(webAddStyle.getImgBar(), Constants.REQUEST_ERROR_3,
		 * "导航栏图片不能为空"); CheckedUtil.isNull(webAddStyle.getImgRecommend(),
		 * Constants.REQUEST_ERROR_4, "推荐理由图片不能为空");
		 * CheckedUtil.isNull(webAddStyle.getImgInspiration(),
		 * Constants.REQUEST_ERROR_5, "创作灵感图片不能为空");
		 * CheckedUtil.isNull(webAddStyle.getImgMakeup(), Constants.REQUEST_ERROR_6,
		 * "专属妆面图片不能为空");
		 */
		TB_Style style = CopyerObject.copyerObject(webAddStyle, TB_Style.class);
		style.setStyleId(IDGenerate.generate());
		style.setStatus(false);
		style.setCreateTime(new Date());
		style.setUpdateTime(new Date());
		if (tb_StyleMapper.insert(style) < 1) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_7, "添加信息失败");
		}
	}

	/**
	 * web端获取风格列表
	 * 
	 * @param getStyleList
	 * @return
	 */
	@Transactional(readOnly = true)
	public Resp_PageInfo<ResponseWebStyleList> getWebStyleList(RequestWebGetStyleList getStyleList) {
		int page = 1;
		if (getStyleList.getPageNum() != null) {
			page = getStyleList.getPageNum();
		}
		PageHelper.startPage(page, Constants.PAGESIZE, true);
		List<ResponseWebStyleList> webStyleLists = new ArrayList<>();
		List<TB_Style> tb_Styles = tb_StyleMapper.getStyleList(getStyleList.getBrandId(), getStyleList.getStatus());
		if (tb_Styles != null && tb_Styles.size() > 0) {
			tb_Styles.stream().forEach(r -> {
				ResponseWebStyleList responseWebStyleList = CopyerObject.copyerObject(r, ResponseWebStyleList.class);
				responseWebStyleList.setIdStr(r.getStyleId().toString());
				webStyleLists.add(responseWebStyleList);
			});
		}
		return CopyPageInfo.copyPageInfo(tb_Styles, webStyleLists);
	}

	/***
	 * web端修改风格
	 * 
	 * @param updateStyle
	 */
	@Transactional
	public void updateStyle(RequestWebUpdateStyle updateStyle) {
		CheckedUtil.isNull(updateStyle.getChineseName(), Constants.REQUEST_ERROR_1, "中文名称不能为空");
		CheckedUtil.isNull(updateStyle.getEnglishName(), Constants.REQUEST_ERROR_2, "英文名称不能为空");
		/*
		 * CheckedUtil.isNull(updateStyle.getImgBar(), Constants.REQUEST_ERROR_3,
		 * "导航栏图片不能为空"); CheckedUtil.isNull(updateStyle.getImgRecommend(),
		 * Constants.REQUEST_ERROR_4, "推荐理由图片不能为空");
		 * CheckedUtil.isNull(updateStyle.getImgInspiration(),
		 * Constants.REQUEST_ERROR_5, "创作灵感图片不能为空");
		 * CheckedUtil.isNull(updateStyle.getImgMakeup(), Constants.REQUEST_ERROR_6,
		 * "专属妆面图片不能为空");
		 */
		CheckedUtil.isNull(updateStyle.getStyleId(), Constants.REQUEST_ERROR_7, "请选择要修改的风格");
		TB_Style tb_Style = tb_StyleMapper.selectByPrimaryKey(updateStyle.getStyleId());
		if (tb_Style == null) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_8, "要修改的风格信息不存在");
		}
		tb_Style = CopyerObject.copyerObject(updateStyle, TB_Style.class);
		tb_Style.setUpdateTime(new Date());
		if (tb_StyleMapper.updateSelective(tb_Style) < 1) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_9, "修改风格信息失败");
		}
	}

	/**
	 * 
	 * 删除风格信息
	 * 
	 * @param delStyle
	 */
	@Transactional
	public void delStyle(RequestWebDelStyle delStyle) {
		CheckedUtil.isNull(delStyle.getStyleId(), Constants.REQUEST_ERROR_1, "请选择要编辑的风格");
		TB_Style tb_Style = tb_StyleMapper.selectByPrimaryKey(delStyle.getStyleId());
		if (tb_Style == null) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_2, "要修改的风格信息不存在");
		}
		tb_Style.setStatus(!tb_Style.getStatus());
		tb_Style.setUpdateTime(new Date());
		if (tb_StyleMapper.updateSelective(tb_Style) < 1) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_3, "删除风格信息失败");
		}
	}

	/**
	 * 
	 * APP获取选取的风格
	 * 
	 * @param requestStyle
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<ResponseStyle> getStyle(RequestStyle requestStyle) {
		CheckedUtil.isNull(requestStyle.getIdList(), Constants.REQUEST_ERROR_1, "风格不能为空");
		List<TB_Style> tb_Styles = tb_StyleMapper.getStyle(requestStyle.getIdList());
		List<ResponseStyle> responseStyles = new ArrayList<>();
		if (tb_Styles != null && tb_Styles.size() > 0) {
			tb_Styles.stream().forEach(r -> {
				ResponseStyle responseStyle = CopyerObject.copyerObject(r, ResponseStyle.class);
				responseStyles.add(responseStyle);
			});
		}
		return responseStyles;

	}

}
